package com.lee9213.conference.track.management.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件读取工具类
 *
 * @author lee9213@163.com
 * @version 1.0
 * @date 2018-07-11 11:29
 */
public final class FileUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileUtil.class);

    public static List<String> readFile(String fileName) throws Exception {
        List<String> textList = new ArrayList<>();
        InputStream inputStream = FileUtil.class.getClassLoader().getResourceAsStream(fileName);
        if (inputStream == null) {
            LOGGER.error("未找到输入文件：{}", fileName);
            throw new FileNotFoundException("未找到输入文件：" + fileName);
        }
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                textList.add(line);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            LOGGER.error("文件读取失败");
            throw new IOException("文件读取失败");
        }
        return textList;
    }
}
